package cn.qlu.yhy.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import cn.qlu.yhy.utils.StringUtil;

@Component
public class Page<T> {

    //Save sum record count;
    private int totalRecord;

    //Save a page have recored count;
    private int pageSize = 10;

    //Save the page current page count;
    private int currentPage = 1;

    //Save the page record number for to select limit;
    private int startRecord = 0;

    //Save sum page count;
    private int totalPageCount = 1;

    //Save the page previous number
    private int previousPage;

    //Save the page next number
    private int nextPage;

    //desc or asc
    private String sequence = "desc";

    //Save ajxa return message
    private String message;

    //Save c r u successful or failure
    private boolean tfMessage = false;

    //Save the page bar
    private int[] pageBar = {1};

    private List<T> list;

    private Map<String, Boolean> deleMess;

    private Map<String, Object> objects;

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        if (totalRecord < 0) {
            this.totalRecord = 0;
        } else {
            this.totalRecord = totalRecord;
        }
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        if (pageSize < 1 || pageSize > 10) {
            this.pageSize = 1;
        } else {
            this.pageSize = pageSize;
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        if (currentPage < 1) {
            this.currentPage = 1;
        } else {
            this.currentPage = currentPage;
        }
    }

    public void setStartRecord(int startRecord) {
        if (startRecord < 0) {
            this.startRecord = 0;
        } else {
            this.startRecord = startRecord;
        }
    }

    public int getStartRecord() {
        return (this.currentPage - 1) * this.pageSize;
    }

    public String getSequence() {
        return sequence;
    }

    public void setSequence(String sequence) {
        if (StringUtil.isEmpty(sequence)) {
            this.sequence = "desc";
        } else if (!sequence.equals("desc") && !sequence.equals("asc")) {
            this.sequence = "desc";
        } else {
            this.sequence = sequence;
        }
    }

    public int getTotalPageCount() {
        this.totalRecord = getTotalRecord();
        this.pageSize = getPageSize();

        if (this.totalRecord % this.pageSize > 0) {

            this.totalPageCount = this.totalRecord / this.pageSize + 1;
        } else {
            this.totalPageCount = this.totalRecord / this.pageSize;
        }

        return totalPageCount;
    }

    public int getPreviousPage() {
        this.currentPage = getCurrentPage();

        this.previousPage = this.currentPage - 1;

        if (this.previousPage < 1) {
            this.previousPage = 1;
        }

        return previousPage;
    }

    public int getNextPage() {
        this.currentPage = getCurrentPage();
        this.totalPageCount = getTotalPageCount();

        this.nextPage = this.currentPage + 1;

        if (this.nextPage > this.totalPageCount) {
            this.nextPage = this.totalPageCount;
        }

        return nextPage;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isTfMessage() {
        return tfMessage;
    }

    public void setTfMessage(boolean tfMessage) {
        this.tfMessage = tfMessage;
    }

    public int[] getPageBar() {
        this.currentPage = getCurrentPage();
        this.previousPage = getPreviousPage();
        this.nextPage = getNextPage();
        this.totalPageCount = getTotalPageCount();

        if (this.totalPageCount == 0) {
            return this.pageBar;
        }

        if (this.totalPageCount < 7) {
            this.pageBar = new int[this.totalPageCount];

            for (int i = 1; i <= this.totalPageCount; i++) {
                this.pageBar[i-1] = i;
            }

        } else {
            this.pageBar = new int[7];

            this.pageBar[0] = 1;
            this.pageBar[6] = this.totalPageCount;

            if (this.currentPage <= 3) {
                for (int i = 1; i <= 5; i++) {
                    this.pageBar[i] = i+1;
                }
            } else if (this.currentPage > 3 && this.currentPage < this.totalPageCount-4) {
                this.pageBar[1] = this.currentPage-2;
                this.pageBar[2] = this.previousPage;
                this.pageBar[3] = this.currentPage;
                this.pageBar[4] = this.nextPage;
                this.pageBar[5] = this.currentPage+2;

            } else if (this.currentPage > this.totalPageCount-6) {
                for (int i = 1; i <= 5; i++) {
                    this.pageBar[i] = this.totalPageCount-(6-i);
                }
            }
        }
        return pageBar;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Map<String, Boolean> getDeleMess() {
        return deleMess;
    }

    public void setDeleMess(Map<String, Boolean> deleMess) {
        this.deleMess = deleMess;
    }

    public Map<String, Object> getObjects() {
        return objects;
    }

    public void setObjects(String key, Object object) {
        if (this.objects == null ) {
            this.objects = new HashMap<String, Object>();
        }
        this.objects.put(key, object);
    }

    public Page() {
        super();
    }

    public Page(boolean tfMessage) {
        super();
        this.tfMessage = tfMessage;
    }

    public Page(String sequence) {
        super();
        if (StringUtil.isEmpty(sequence)) {
            this.sequence = "desc";
        } else if (!sequence.equals("desc") && !sequence.equals("asc")) {
            this.sequence = "desc";
        } else {
            this.sequence = sequence;
        }
    }

    public Page(String message, int i) {
        super();
        this.message = message;
    }

    public Page(String pageS, String currentP) {

        if (StringUtil.isEmpty(pageS)) {
            this.pageSize = 10;
        } else if (Integer.parseInt(pageS) > 10 || Integer.parseInt(pageS) < 1) {
            this.pageSize = 1;
        } else {
            this.pageSize = Integer.parseInt(pageS);
        }

        if (StringUtil.isEmpty(currentP)) {
            this.currentPage = 1;
        } else {
            if (Integer.parseInt(currentP) > this.totalPageCount) {
                this.currentPage = 1;
            }
            this.currentPage = Integer.parseInt(currentP);
        }
    }

    public Page(String pageS, String currentP, String sequence) {
        super();

        if (StringUtil.isEmpty(pageS)) {
            this.pageSize = 10;
        } else if (Integer.parseInt(pageS) > 10 || Integer.parseInt(pageS) < 1) {
            this.pageSize = 1;
        } else {
            this.pageSize = Integer.parseInt(pageS);
        }

        if (StringUtil.isEmpty(currentP)) {
            this.currentPage = 1;
        } else {
            if (Integer.parseInt(currentP) > this.totalPageCount) {
                this.currentPage = 1;
            }
            this.currentPage = Integer.parseInt(currentP);
        }

        if (StringUtil.isEmpty(sequence)) {
            this.sequence = "desc";
        } else if (!sequence.equals("desc") && !sequence.equals("asc")) {
            this.sequence = "desc";
        } else {
            this.sequence = sequence;
        }
    }
}